package com.wutuo.wtservice.provider.service;

import cn.apestar.core.base.BaseService;
import cn.apestar.core.base.GenericMapper;
import cn.apestar.util.CommonUtil;
import com.wutuo.wtservice.api.UserServiceAPIService;
import com.wutuo.wtservice.api.request.ListUserServiceRequest;
import com.wutuo.wtservice.api.request.UserServiceRequest;
import com.wutuo.wtservice.api.response.ServiceEnumResponse;
import com.wutuo.wtservice.api.response.UserServiceResponse;
import com.wutuo.wtservice.provider.dao.UserServiceMapper;
import com.wutuo.wtservice.provider.domain.WtUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 服务关系表 <br>
 *
 * @author lvh
 * @version 1.0
 */
@Service
public class UserServiceService extends BaseService<WtUserService, Long>
        implements UserServiceAPIService {

    private Logger logger = LoggerFactory.getLogger(UserServiceService.class);

    @Autowired
    private UserServiceMapper userServiceMapper;

    @Override
    protected GenericMapper<WtUserService, Long> getMapper() {

        // TODO Auto-generated method stub
        return userServiceMapper;
    }

    public void addorUpdate(UserServiceRequest record) {

        if (record.getId() == null) {
            super.add(record);
        } else
            super.update(record);
    }

    public void del(Long id) {

        UserServiceRequest model = new UserServiceRequest();
        model.setId(id);
        super.delete(model);
    }

    /**
     * 服务分页查询
     *
     * @param request
     * @return List<UserServiceResponse>
     * @author lvh 2016年2月3日 下午1:01:17</br>
     */
    public List<UserServiceResponse> lisForPage(ListUserServiceRequest request) {

        List<UserServiceResponse> list = userServiceMapper.lisForPage(request);
        return list;
    }

    /**
     * 服务查询
     *
     * @param request
     * @return List<UserServiceResponse>
     * @author lvh 2016年2月3日 下午1:01:17</br>
     */
    public List<UserServiceResponse> list(UserServiceRequest request) {

        request.setDelFlag(true);
        return userServiceMapper.list(request);
    }

    /**
     * 删除服务信息
     *
     * @param request
     * @return List<UserServiceResponse>
     * @author lvh 2016年2月3日 下午1:01:17</br>
     */
    public void deleteUserServiceByrosterId(Long rosterId) {

        // TODO Auto-generated method stub
        userServiceMapper.deleteUserServiceByrosterId(rosterId);
    }

    /**
     * 获取服务信息
     *
     * @param <T>
     * @param list
     * @param tKey
     * @param skey
     * @return void
     * @author lvh 2016年2月3日 下午1:24:45</br>
     */
    public <T> void getUseServiceByRoasterId(List list, String tKey, String skey) {

        // TODO Auto-generated method stub
        StringBuffer idIn = CommonUtil.getLongInt(list, tKey);
        UserServiceRequest request = new UserServiceRequest();
        request.setIds(idIn.toString());
        List<UserServiceResponse> listUser = list(request);

        List<ServiceEnumResponse> listWtService = createServiceEnumResponse(listUser);
        Map map = CommonUtil.listToObject(listWtService, "dataId");
        for (int i = 0; i < list.size(); i++) {
            Object modelS = list.get(i);
            Object o = CommonUtil.getProperty(modelS, tKey);
            if (o == null) {
                continue;
            }
            String id = o.toString();
            String[] ids = id.split(",");
            List<T> s = new ArrayList();
            for (int j = 0; j < ids.length; j++) {
                if (map.containsKey(ids[j])) {
                    if (!s.contains(map.get(ids[j])))
                        s.add((T) map.get(ids[j]));
                }
            }
            CommonUtil.setProperty(modelS, skey, s);
        }
    }

    public List<ServiceEnumResponse> createServiceEnumResponse(List<UserServiceResponse> list) {

        List<ServiceEnumResponse> listResult = new ArrayList();
        for (int i = 0; i < list.size(); i++) {
            UserServiceResponse m = list.get(i);
            ServiceEnumResponse s = new ServiceEnumResponse();
            s.setId(m.getWtServiceId());
            s.setName(m.getTypeName());
            s.setTypeImage(m.getTypeImage());
            s.setDataId(m.getId());
            listResult.add(s);
        }
        return listResult;
    }

}